package com.DongBing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.val;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.DongBing.common.utils.PageUtils;
import com.DongBing.common.utils.Query;

import com.DongBing.dao.AttrGroupDao;
import com.DongBing.entity.AttrGroupEntity;
import com.DongBing.service.AttrGroupService;
import org.springframework.util.ObjectUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, String cateLogId) {
        if (ObjectUtils.isEmpty(cateLogId)) {
            val page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<>()
            );
            return new PageUtils(page);
        }else {
            val wrapper = new LambdaQueryWrapper<AttrGroupEntity>();
            val key = (String) params.get("key");
            wrapper.eq(!ObjectUtils.isEmpty(cateLogId),AttrGroupEntity::getCatelogId,cateLogId);
            wrapper.and(!ObjectUtils.isEmpty(key),(obj)->{
                obj.eq(AttrGroupEntity::getAttrGroupId,key);
                obj.or().like(AttrGroupEntity::getAttrGroupName,key);
            });
            val page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

}
